package models

import (
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"yklx6/appconfig"
)

var DB *gorm.DB

func MysqlInit() {
	var err error
	mysqkls := appconfig.AppConfig.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
		mysqkls.User,
		mysqkls.Password,
		mysqkls.Host,
		mysqkls.Port,
		mysqkls.Database,
	)
	DB, err = gorm.Open(mysql.Open(dsn))
	if err != nil {
		fmt.Println("数据库连接失败")
		return
	} else {
		fmt.Println("数据库连接成功")
	}
	err = DB.AutoMigrate(&User{}, &Role{}, &Userrolegxb{}, &Quan{}, &Rolequanhxb{}, &Comans{}, &Types{}, &Orders{})
	if err != nil {
		fmt.Println("迁移失败")
		return
	} else {
		fmt.Println("迁移成功")
	}
}
